package cruciverba.model;

import java.util.StringTokenizer;


public class ContaCaratteriComuni implements IVerifica {

	@Override
	public int verify(String string) throws Exception {
		
		int commonChar = 0;
		
		string = string.toLowerCase();
		StringTokenizer strtok = new StringTokenizer(string, ",");

		if(strtok.countTokens() != 2) {
			throw new Exception("Separate the words with a comma");
		}
		
		String string1 = strtok.nextToken();
		String string2 = strtok.nextToken();
		
		string1 = string1.trim();
		string2 = string2.trim();
		

		CruciverbaUtils.isWord(string1);
		CruciverbaUtils.isWord(string2);
		
		String stringtmp = CruciverbaUtils.deleteMultipleOccurrencies(string1);
		
		int length = stringtmp.length();
		
		for (int i = 0; i < length; i++) {
			if(string2.contains(String.valueOf(stringtmp.charAt(i)))){
				commonChar++;
			}
		}
		
		return commonChar;
	}


	@Override
	public String modellaVerify(String string) throws Exception {
		return Integer.toString(verify(string));
	}



}
